$(function () {
    $.ajax({
        url: "/o2o-merchant/sec/rest/merchant",
        type: "get"
    }).done(function (msg) {
        $("#userPortrait").attr("src",msg.portrait);
    });
    $.ajax({
        url: "/o2o-merchant/sec/rest/foodType",
        type: "get"
    }).done(function (msg) {
        msg.forEach(function (element) {
            $("select[name='foodTypeId']").each(function (index, domEle) {
                $(domEle).append($("<option>").val(element.id).text(element.typeName));
            });
        });

    });
    $("#addFoodBtn").on("click", function () {
        var formData = new FormData($("#addFoodForm").get(0));
        if($("#addImage").val()==""||$("#addImage").val()==null){
            return ;
        }
        $.ajax({
            url: "/o2o-merchant/sec/rest/food",
            type: "post",
            data: formData,
            contentType: false,
            processData: false
        }).done(function (msg) {
            if (msg) {
                $("#addModel").modal('hide');
                render();
            }
        });
    });

    function render() {
        $.ajax({
            url: "/o2o-merchant/sec/rest/food",
            type: "get"
        }).done(function (msg) {
            var foodList = $("#foodList").empty();
            msg.forEach(function (element) {
                var food = $('<div class="col-md-3 col-md-offset-1 panel panel-success"><div class="panel-heading" align="center"><strong id="name"></strong></div><div class="panel-body"><div class="caption" align="center"><img width="120" height="120"  alt="菜品图片信息无法显示" class="img-rounded"/><h4><label>类型:&nbsp;</label><label id="typeName"></label></h4><h4><label >价格:&nbsp;</label><label id="price"></label><label>￥</label></h4><p><a type="button" id="updateBtn" class="btn btn-success" >更新</a><a id="deleteBtn" type="button" class="btn btn-success">删除</a><a id="recommentBtn" type="button" class="btn btn-success" >推荐</a></p></div></div>').appendTo(foodList);
                //data-target="#updateModal" data-toggle="modal"data-target="#recommendModal" data-toggle="modal"
                food.find("#name").text(element.name);
                food.find("img").attr("src", element.image);
                food.find("#typeName").text(element.foodType.typeName);
                food.find("#price").text(element.price);

                food.find("#updateBtn").on("click", function () {
                    $("#updateModel").modal('show');
                    $("#hiddenId").val(element.id);
                    $("#foodName2").val(element.name);
                    console.log($("#hiddenId"));
                });
                food.find("#deleteBtn").on('click', function () {
                    $.ajax({
                        url: "/o2o-merchant/sec/rest/food/" + element.id,
                        type: "delete"
                    }).done(function () {
                        render();
                    });
                });

                food.find("#recommentBtn").on('click', function () {
                    $("#recommendModal").modal('show');
                    $("#hiddenFoodId").val(element.id);
                })
            });

        });

    }
    render();
    $("#updateFoodBtn").on("click",function(){
        if($("#updateImage").val()==""||$("#updateImage").val()==null){
            return ;
        }
        console.log("updateFood");
        var formData = new FormData($("#updateFoodForm").get(0));
        $.ajax({
            url:"/o2o-merchant/sec/rest/food/update.action",
            type:"post",
            data: formData,
            contentType: false,
            processData: false
        }).done(function(msg){
            if(msg){
                $("#updateModel").modal("hide");
                render();
            }
        });

    });

    $("#recommendBtn").on("click",function(){
        var formData = new FormData($("#recommendForm").get(0));
        for(let pair of formData.entries()){
            console.log(pair[0]+','+pair[1]);
        }
        $.ajax({
            url:"/o2o-merchant/sec/rest/advertisement",
            type:"post",
            data: formData,
            contentType: false,
            processData: false
        }).done(function(msg){
            if(msg){
                $("#recommendModal").modal('hide');
                alert("success!");
            }else{
                $("#recommendModal").modal('hide');
                alert("fail!");
            }
        });
    });

})

